C++ glog示例

您所在的位置:网站首页 cmake glog C++ glog示例

C++ glog示例

#C++ glog示例| 来源: 网络整理| 查看: 265

1、下载、安装:

方式一:从github上下载源码、编译、安装:​​https://github.com/google/glog​​

$ cd /usr/local$ git clone https://github.com/google/glog.git$ cd glog$ ./autogen.sh && ./configure && make && make install

执行过程:--------------------------------------------------------------------- /usr/bin/mkdir -p '/usr/local/share/doc/glog-0.4.0' /usr/bin/install -c -m 644 AUTHORS COPYING ChangeLog INSTALL README.md README.windows doc/designstyle.css doc/glog.html '/usr/local/share/doc/glog-0.4.0' /usr/bin/mkdir -p '/usr/local/include/glog' /usr/bin/install -c -m 644 src/glog/log_severity.h '/usr/local/include/glog' /usr/bin/mkdir -p '/usr/local/include/glog' /usr/bin/install -c -m 644 src/glog/logging.h src/glog/raw_logging.h src/glog/vlog_is_on.h src/glog/stl_logging.h '/usr/local/include/glog' /usr/bin/mkdir -p '/usr/local/lib/pkgconfig' /usr/bin/install -c -m 644 libglog.pc '/usr/local/lib/pkgconfig'make[1]: Leaving directory `/usr/local/glog'

可以看到对应的头文件、库文件已经放到了/usr/local/include/和/usr/local/lib下面了。

方式二:下载tar包​

然后执行install.sh:

#!/bin/bash

cd $(dirname $0)

# should run as a rootuser=`whoami`if [ "$user" != "root" ]; then echo "Please run as root" exit 1fi

export PATH="/usr/local/bin/:$PATH"# Disable debug modeexport CPPFLAGS="-DNDEBUG"

all_parameters="$@"

function check_install_status() { if [ $? -ne 0 ]; then echo -e "\033[31mERROR: install failed.\033[0m" exit 1 fi}

#install glogif [ ! -d glog-0.3.5 ]; then tar -zxf glog-0.3.5.tar.gz cd glog-0.3.5/ ./configure check_install_status make -j8 check_install_status make install check_install_status ldconfig cd ../fi

注:这时 glog 库会默认安装在 /usr/local/lib/ 下,头文件放在 /usr/local/include/glog/ 中。

2、菜鸟级:

1)代码:test1.cpp

#include

int main(int argc,char* argv[]){ google::InitGoogleLogging(argv[0]); //初始化 glog LOG(INFO) a.out.localhost.localdomain.roo.log.INFO.20190824-172834.9600-rw-r--r--. 1 root root 206 Aug 24 17:28 a.out.localhost.localdomain.roo.log.INFO.20190824-172834.9600

# cat /tmp/a.out.INFO Log file created at: 2019/08/24 17:28:34Running on machine: localhost.localdomainLog line format: [IWEF]mmdd hh:mm:ss.uuuuuu threadid file:line] msgI0824 17:28:34.898052 9600 test1.cpp:5] hello,glog...

3.2)日志文件名的格式为: ...log....

3.3)日志格式:I+日期 时:分:秒.微秒 线程号 源文件名:行数] 信息

3.4)glog 支持四种日志级别,INFO、WARNING、ERROR和FATAL。不同级别的日志信息会输出到不同文件,同时高级别的日志也会写入到低级别中。默认情况下,在打印完FATAL日志之后,程序将会终止。ERROR和FATAL的日志除了会写到日志中,还会输出到 stderr。

3、新手级:

3.1)指定日志位置:

日志文件的位置可以通过 gflags 命令行参数 log_dir 来指定。有两种方式:

在 main 函数开始时初始化 gflags: google::ParseCommandLineFlags(&argc, &argv, true);,然后命令行指定log_dir(推荐)直接在代码里 指定 FLAGS_log_dir 的值。

看一个例子:

[root@localhost test_glog]# cat test1.cpp #include #include

int main(int argc,char* argv[]){ gflags::ParseCommandLineFlags(&argc, &argv, true); google::InitGoogleLogging(argv[0]); LOG(INFO)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3